package com.fare.taluo.core.domain.config.city;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Cacheable;
import javax.persistence.Column;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.dayatang.domain.Entity;
import com.dayatang.domain.InstanceFactory;
import com.dayatang.domain.QuerySettings;
import com.fare.taluo.base.ddd.domain.baseentity.BaseEntity;
import com.fare.taluo.vo.core.CityEntityVO;
/**
 * 地区，城市信息
 * <p>一个结构树，包括大洲，国家，地区，城市等</br>
 * 一个城市下会有多个机场
 * @author xiangsf 2014-3-19
 *
 */
@javax.persistence.Entity
@Table(name = "FDC_CITY")
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class CityEntity extends BaseEntity{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -6722480404511580993L;
	
	public final static Map<Integer, String> cityTypes = new HashMap<Integer, String>();
	public final static Integer ISLAND = 1;
	public final static Integer ISLAND_AREA = 2;
	public final static Integer COUNTRY = 4;
	public final static Integer COUNTRY_AREA = 8;
	public final static Integer PROVINCE = 16;
	public final static Integer CITY = 32;
	public final static Integer CITY_AREA = 64;
	public final static Integer PORT = 128;
	static{
		cityTypes.put(ISLAND, "大洲");
		cityTypes.put(ISLAND_AREA, "洲区域");
		cityTypes.put(COUNTRY, "国家(地区)");
		cityTypes.put(COUNTRY_AREA, "国家区域");
		cityTypes.put(PROVINCE, "省份(州或区、直辖市)");
		cityTypes.put(CITY, "城市(县)");
		cityTypes.put(CITY_AREA, "区");
		cityTypes.put(PORT, "机场");
	}
	
	/**
	 * 树节点的上级ID
	 */
	@Column(name = "pid", insertable=false, updatable=false)
	private Long pid;
	/**
	 * 国家、城市或地区名称
	 */
	@Column(name = "NAME")
	private String name;
	/**
	 * 别名
	 */
	@Column(name = "ALIAS_NAME")
	private String aliasName;
	/**
	 * 国家、城市或地区编码
	 */
	@Column(name = "CODE")
	private String code;
	/**
	 * 城市拼音全拼或英文名
	 */
	@Column(name ="NAME_SPELL")
	private String nameSpell;
	/**
	 * 城市拼音简写或英文名简写
	 */
	@Column(name ="NAME_SPELL_INITIALS")
	private String nameSpellInitials;
	/**
	 * 节点类型(国家，城市，区域等)
	 */
	@Column(name ="TREE_TYPE")
	private Integer treeType = CITY;
	/**
	 * 层级
	 */
	@Column(name ="TREE_LEVEL")
	private Integer treeLevel;
	/**
	 * 排序
	 */
	@Column(name ="TREE_SORT")
	private Integer treeSort;
	/**
	 * 热门等级,0为非热门，>0为热门
	 */
	@Column(name ="HOT_LEVEL")
	private Integer hotLevel = 1;
	/**
	 * 创建时间
	 */
	@Column(name ="CREATE_DATE")
	private Date createDate = new Date();
	/**
	 * 最后更新日期
	 */
	@Column(name ="UPDATE_DATE")
	private Date updateDate;
	/**
	 * 1为国际，0为国内
	 */
	@Column(name = "IS_INTERNATIONAL")
	private Integer isInternational = 0;
	/**
	 * 状态，１可用，０不可用
	 */
	@Column(name = "STATE")
	private Integer state ;
	/**
	 * 所属父级对象
	 */
	@JoinColumn(name = "PID", referencedColumnName = "ID")
	@ManyToOne
	@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
	private CityEntity parent;
	
	public CityEntity getParent() {
		return parent;
	}

	public void setParent(CityEntity parent) {
		this.parent = parent;
	}

	public Integer getState() {
		return state;
	}

	public void setState(Integer state) {
		this.state = state;
	}

	private static CityEntityRepository getCityEntityRepository(){
		return InstanceFactory.getInstance(CityEntityRepository.class);
	}
	public Map<String,CityEntity> findCityToMap(){
		List<CityEntity> list = this.findAll();
		Map<String, CityEntity> map = new HashMap<String, CityEntity>();
		for (CityEntity p : list) {
			if(p.getTreeType() == CITY)//只要城市
				map.put(p.getCode(), p);
		}	
		return map;
	}
	public List<CityEntity> findCityEntityByCityCode(String cityCode){
		return getCityEntityRepository().findCityEntityByCityCode(cityCode);
	}
	
	public List<CityEntity> findAll(){
		return getCityEntityRepository().findAll();
	};
	
	public List<CityEntity> findCityByConditions(String name,String code){
		return getCityEntityRepository().findCityByConditions(name, code);
	};
	
	public CityEntity getCity(Long id){
		return getCityEntityRepository().get(CityEntity.class, id);
	}
	
	public void saveInternationalImg(String area, String name) {
		getCityEntityRepository().saveInternationalImg(area, name);		
	}

	
	public void saveOrUpdate(CityEntity cityEntity){
		getCityEntityRepository().saveOrUpdate(cityEntity);
	};
	
	public void delete(CityEntity cityEntity){
		getCityEntityRepository().delete(cityEntity);
	}
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	
	public String getAliasName() {
		return aliasName;
	}

	public void setAliasName(String aliasName) {
		this.aliasName = aliasName;
	}

	public Integer getIsInternational() {
		return isInternational;
	}

	public void setIsInternational(Integer isInternational) {
		this.isInternational = isInternational;
	}


	
	public String getNameSpell() {
		return nameSpell;
	}

	public void setNameSpell(String nameSpell) {
		this.nameSpell = nameSpell;
	}

	public String getNameSpellInitials() {
		return nameSpellInitials;
	}

	public void setNameSpellInitials(String nameSpellInitials) {
		this.nameSpellInitials = nameSpellInitials;
	}
	
	public Long getPid() {
		return pid;
	}

	public void setPid(Long pid) {
		this.pid = pid;
	}

	public Integer getTreeLevel() {
		return treeLevel;
	}

	public void setTreeLevel(Integer treeLevel) {
		this.treeLevel = treeLevel;
	}

	public Integer getTreeSort() {
		return treeSort;
	}

	public void setTreeSort(Integer treeSort) {
		this.treeSort = treeSort;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}

	public List<CityEntity> findCityByIorChina(int flag) {
		return getCityEntityRepository().findCityByIorChina(flag);
	}


	public Integer getHotLevel() {
		return hotLevel;
	}

	public void setHotLevel(Integer hotLevel) {
		this.hotLevel = hotLevel;
	}

	@Override
	public boolean isNew() {
		return getId() == null || getId() == 0L;
	}
	
	public Integer getTreeType() {
		return treeType;
	}

	public void setTreeType(Integer treeType) {
		this.treeType = treeType;
	}

	public void loadByVo(CityEntityVO vo){
		this.id = vo.getId();
		this.code = vo.getCode();
		this.createDate = vo.getCreateDate();
		this.isInternational = vo.getIsInternational();
		this.name = vo.getName();
		this.nameSpell = vo.getNameSpell();
		this.nameSpellInitials =  vo.getNameSpellInitials();
		this.pid = vo.getPid();
		this.state = vo.getState();
		this.treeLevel = vo.getTreeLevel();
		this.updateDate = vo.getUpdateDate();
		this.treeSort = vo.getTreeSort();
	}

	@Override
	public String toString() {
		return "CityEntity [pid=" + pid + ", name=" + name + ", code=" + code
				+ ", nameSpell=" + nameSpell + ", nameSpellInitials="
				+ nameSpellInitials + ", treeType=" + treeType + ", treeLevel="
				+ treeLevel + ", treeSort=" + treeSort + ", hotLevel="
				+ hotLevel + ", createDate=" + createDate + ", updateDate="
				+ updateDate + ", isInternational=" + isInternational
				+ ", state=" + state + "]";
	}
}
